import { atom, useAtom } from "jotai"
import { atomWithStorage } from "jotai/utils"
import { withImmer } from "jotai-immer"
import { genStorageKey } from "@/utils/common"

type verifyAtomType = {
    otpId?: string
    type?: "edit" | "create" | "forget"
    phone?: string
    email?: string
    field?: "email" | "phone" | "password"
    verifyMethod?: "email" | "phone" | "service"
    code?: string //新增验证码
    oldPassword?: string //旧密码
}

const key = genStorageKey("verifyAtom")
export const verifyAtom = withImmer(atomWithStorage<verifyAtomType | null>(key, null))

export const SetVerifyAtom = withImmer(
    atom(null, (get, set, data: verifyAtomType) => {
        set(verifyAtom, {
            ...get(verifyAtom),
            ...data,
        })
    }),
)

export const SetInitVerifyAtom = withImmer(
    atom(null, (get, set) => {
        set(verifyAtom, null)
    }),
)
